package com.nd.android.u.ui.widge.chatfragment;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.common.android.utils.DisplayUtil;
import com.nd.android.u.business.db.dao.QuickReplyDao;
import com.nd.android.u.chatInterfaceImpl.ChatInterfaceImpl;
import com.nd.android.u.controller.bean.QuickReply;
import com.nd.android.u.controller.bean.QuickReplyList;
import com.nd.android.u.controller.factory.ChatDaoFactory;
import com.nd.android.u.ui.ChatEntry;
import com.nd.android.u.ui.R;
import com.nd.android.u.ui.activity.chat_relative_quickreply.QuickReplyAcitivty;
import com.nd.android.u.ui.activity.chat_relative_quickreply.QuickReplyAdapter;
import com.product.android.commonInterface.weibo.FlowerMessageInfo;

public class PopQuickReply {
	public static final int REPLY_TYPE_CHAT = 0;
	public static final int REPLY_TYPE_ACKNOWLEDGE = 1;
	public static final int REPLY_TYPE_PRESENT = 2;
	/** 界面上显示的最多的快捷回复数量 */
	private static final int MAX_ITEM = 5;
	private int mReplyType;

	public interface onSwitchToEditListener {
		// 在弹窗界面进入快捷回复编辑
		void onSwicthToEdit();
	}

	private onSwitchToEditListener mSwitchToEditListener;

	public interface onItemSelectListener {
		// 选中某个快捷回复
		void onItemSelect(String text);
	}

	private onItemSelectListener mItemSelectListener;
	private Context mContext;
	private ListView lvQuickReply;
	private PopupWindow mWindow;
	private QuickReplyList mReplyList;
	private QuickReplyDao mQuickReplyDao;
	private int mItemHeight;
	private boolean mbIsToEdit = false;
	private QuickReplyAdapter mQuickReplyAdapter;
	private long mFid;
	private String[] mPostQuickReplies;

	public PopQuickReply(Context context, int type, long fid) {
		mContext = context;
		this.mReplyType = type;
		this.mFid = fid;
	}

	public void setOnSwitchToEditListener(onSwitchToEditListener listener) {
		mSwitchToEditListener = listener;
	}

	public void setOnItemSelectListener(onItemSelectListener listener) {
		mItemSelectListener = listener;
	}

	@TargetApi(Build.VERSION_CODES.CUPCAKE)
	@SuppressWarnings("deprecation")
	private void buildWindow(View anchor) {
		getReplyTexts();
		LinearLayout ll = new LinearLayout(mContext);
		ll.setPadding(2, 2, 2, 2);
		lvQuickReply = new ListView(mContext);
		mQuickReplyAdapter = new QuickReplyAdapter(mContext, mReplyList);
		lvQuickReply.setAdapter(mQuickReplyAdapter);
		lvQuickReply.setOnItemClickListener(itemClickListener);
		// 获取单个item的高度
		mItemHeight = DisplayUtil.dip2px(mContext, QuickReplyAdapter.ITEM_HEIGHT);
		int size = mReplyList.size();
		int h = mItemHeight * size;
		int maxHeight = anchor.getHeight();
		if (mSwitchToEditListener == null) {
			// 在聊天界面（非弹窗界面），允许的最大高度为listview的一半
			maxHeight = Math.min(size, MAX_ITEM) * mItemHeight;
		}
		h = Math.min(h, maxHeight);
		// 设定宽度为listview的宽度的一半
		// int w=anchor.getWidth()/2;
		// 宽度改为19/20 update by ZXQ
		int w = anchor.getWidth() * 19 / 20;
		ll.addView(lvQuickReply);
		ll.setBackgroundColor(0xff888888);
		lvQuickReply.setBackgroundResource(R.drawable.activity_bg);
		mWindow = new PopupWindow(ll, w, h + 4);
		mWindow.setBackgroundDrawable(new BitmapDrawable());
		mWindow.setFocusable(true);
		mWindow.setOutsideTouchable(true);
	}

	public void setQuickReply(String[] replies) {
		mPostQuickReplies = replies;
	}

	private void getReplyTexts() {
		// 送花答谢快捷回复
		if (mReplyType == REPLY_TYPE_ACKNOWLEDGE) {
			String[] defaults = mContext.getResources().getStringArray(R.array.quick_acknowledge);
			mReplyList = new QuickReplyList();
			for (int i = 0, len = defaults.length; i < len; i++) {
				QuickReply reply = new QuickReply();
				if (defaults[i].contains("XXX")) {
					reply.setText(defaults[i].replaceAll("XXX", ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getUserName(mFid)));
				} else {
					reply.setText(defaults[i]);
				}
				reply.setOrder(i);
				mReplyList.add(reply);
			}
			// 判断是否有设置过送花答谢留言
			FlowerMessageInfo info = ChatEntry.INSTANCE.chatCallOtherModel_UI.getThanksFlowerMessage();
			if (info != null) {
				QuickReply set = new QuickReply();
				set.setText(mContext.getString(R.string.quick_reply_acknowledge));
				mReplyList.add(0, set);
			}
			return;
		}
		if (mPostQuickReplies != null) {
			int size = mPostQuickReplies.length;
			if (size == 0) {
				mPostQuickReplies = mContext.getResources().getStringArray(R.array.quick_present);
			}
			mReplyList = new QuickReplyList();
			int i = 0;
			for (String str : mPostQuickReplies) {
				QuickReply reply = new QuickReply();
				reply.setText(str);
				reply.setOrder(i);
				i++;
				mReplyList.add(reply);
			}
			// 判断是否有设置过送花答谢留言
			FlowerMessageInfo info = ChatEntry.INSTANCE.chatCallOtherModel_UI.getThanksFlowerMessage();
			if (info != null) {
				QuickReply set = new QuickReply();
				set.setText(mContext.getString(R.string.quick_reply_acknowledge));
				mReplyList.add(0, set);
			}
		} else {
			mQuickReplyDao = ChatDaoFactory.getInstance().getQuickReplyDao();
			mReplyList = mQuickReplyDao.getReply();
			if (mReplyList == null || mReplyList.size() == 0) {
				String[] defaults = mContext.getResources().getStringArray(R.array.quick_reply);
				mReplyList = new QuickReplyList();
				for (int i = 0, len = defaults.length; i < len; i++) {
					QuickReply reply = new QuickReply();
					reply.setText(defaults[i]);
					reply.setOrder(i);
					mReplyList.add(reply);
					mQuickReplyDao.insertReply(reply);
				}
			}
			QuickReply set = new QuickReply();
			set.setText(mContext.getString(R.string.quick_reply_set));
			mReplyList.add(set);
		}
	}

	public void refresh() {
		if (mbIsToEdit) {
			mWindow = null;
			mbIsToEdit = false;
		}
	}

	public void show(View anchor, int xOffset, int yOffset) {
		if (mWindow == null) {
			buildWindow(anchor);
		}
		mWindow.showAtLocation(anchor, Gravity.BOTTOM | Gravity.RIGHT, xOffset, yOffset);
	}

	public void dismiss() {
		mWindow.dismiss();
	}

	private OnItemClickListener itemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			// 只有普通聊天的快捷回复才进入快捷回复设置
			if (mReplyType == REPLY_TYPE_CHAT && arg2 == mReplyList.size() - 1) {
				if (mSwitchToEditListener != null) {
					mSwitchToEditListener.onSwicthToEdit();
				} else {
					// 未专门设置一个监听，直接进入快捷回复编辑界面
					Intent intent = new Intent(mContext, QuickReplyAcitivty.class);
					((Activity) mContext).startActivity(intent);
					mbIsToEdit = true;
				}
			} else {
				if (mItemSelectListener != null) {
					mItemSelectListener.onItemSelect(mReplyList.get(arg2).getText());
				}
			}
			dismiss();
		}
	};

	/**
	 * mReplyType
	 * 
	 * @return the mReplyType
	 * @since CodingExample Ver 1.0
	 */
	public int getmReplyType() {
		return mReplyType;
	}

	/**
	 * mReplyType
	 * 
	 * @param mReplyType
	 *            the mReplyType to set
	 * @since CodingExample Ver 1.0
	 */
	public void setmReplyType(int mReplyType) {
		this.mReplyType = mReplyType;
	}
}
